package com.ruoyi.template.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import com.ruoyi.template.service.IVtsTemplateCodeDeptService;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.template.domain.vo.VtsTemplateCodeVo;
import com.ruoyi.template.domain.bo.VtsTemplateCodeBo;
import com.ruoyi.template.service.IVtsTemplateCodeService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 表单代码
 *
 * @author fjw
 * @date 2023-08-25
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/template/template")
public class VtsTemplateCodeController extends BaseController {

    private final IVtsTemplateCodeService iVtsTemplateCodeService;
    private final IVtsTemplateCodeDeptService iVtsTemplateCodeDeptService;

    /**
     * 查询表单代码列表
     */
    @SaCheckPermission("template:template:list")
    @GetMapping("/list")
    public TableDataInfo<VtsTemplateCodeVo> list(VtsTemplateCodeBo bo, PageQuery pageQuery) {
        return iVtsTemplateCodeService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出表单代码列表
     */
    @SaCheckPermission("template:template:export")
    @Log(title = "表单代码", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(VtsTemplateCodeBo bo, HttpServletResponse response) {
        List<VtsTemplateCodeVo> list = iVtsTemplateCodeService.queryList(bo);
        ExcelUtil.exportExcel(list, "表单代码", VtsTemplateCodeVo.class, response);
    }

    /**
     * 获取表单代码详细信息
     *
     * @param formId 主键
     */
    @SaCheckPermission("template:template:query")
    @GetMapping("/{formId}")
    public R<VtsTemplateCodeVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long formId) {
        return R.ok(iVtsTemplateCodeService.queryById(formId));
    }

    /**
     * 新增表单代码
     */
    @SaCheckPermission("template:template:add")
    @Log(title = "表单代码", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody VtsTemplateCodeBo bo) {
        System.out.println(bo);
        return toAjax(iVtsTemplateCodeService.insertByBo(bo));
    }

    /**
     * 修改表单代码
     */
    @SaCheckPermission("template:template:edit")
    @Log(title = "表单代码", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody VtsTemplateCodeBo bo) {
        return toAjax(iVtsTemplateCodeService.updateByBo(bo));
    }

    /**
     * 删除表单代码
     *
     * @param formIds 主键串
     */
    @SaCheckPermission("template:template:remove")
    @Log(title = "表单代码", businessType = BusinessType.DELETE)
    @DeleteMapping("/{formIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] formIds) {
        return toAjax(iVtsTemplateCodeService.deleteWithValidByIds(Arrays.asList(formIds), true));
    }

    /**
     * 新增表单代码
     */
    @SaCheckPermission("template:template:publish")
    @Log(title = "发布表单", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/publish")
    public R<Void> publish(@RequestBody Long formId) {
        return iVtsTemplateCodeDeptService.publish(formId);
    }


    /**
     * 获取当前用户的模板id（根据部门来分的）
     */
    @SaCheckPermission("template:template:getFormIds")
    @GetMapping("/getFormIds")
    public R<List<Long>> getFormIds() {
        return R.ok(iVtsTemplateCodeDeptService.getFormIds());
    }
}
